// pages/store/order/index.js
import {
  getStoreOrderApi,
  postStoreOrderConfirmApi,
  postStoreOrderPayApi,
  postStoreOrderRefundApi
} from '../../../https/store'
import {
  getInfoApi
} from '../../../https/user'
import {
  BASE_URL
} from '../../../constant/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    order: {},
    showPay: false,
    info: {
      balance: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const orderId = options.docid
    this.getOrder(orderId)
    this.getInfo()
  },
  // 获取用户数据
  async getInfo() {
    const getRes = await getInfoApi()
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        info: getRes.data
      })
    }
  },
  // 获取订单
  async getOrder(orderId) {
    const getRes = await getStoreOrderApi({
      orderId
    })
    console.log(getRes.data)
    if (getRes.code == 200) {
      this.setData({
        order: getRes.data
      })
    }
  },
  // 提示不支持该支付方式
  showToast() {
    wx.showToast({
      title: '暂不支持微信支付',
      icon: 'error'
    })
  },
  // 打开支付弹框
  openShowPay() {
    this.setData({
      showPay: true
    })
  },
  // 关闭弹框
  onClose() {
    this.setData({
      showPay: false
    })
  },
  // 复制快递单号
  copyText() {
    const {
      tracking,
      express
    } = this.data.order
    wx.setClipboardData({
      data: tracking,
      success(res) {
        wx.showToast({
          title: '复制成功'
        });
      }
    });
  },
  // 支付按钮
  async payMeoney() {
    console.log(this.data)
    const orderId = this.data.order.id
    wx.showLoading({
      title: '支付中..',
      mask: true
    })
    const postRes = await postStoreOrderPayApi({
      orderId
    })
    console.log(postRes)
    if (postRes.code == 200) {
      setTimeout(async () => {
        await this.getOrder(orderId)
        await this.getInfo()
        this.onClose()
        wx.showToast({
          title: '支付成功',
          icon: 'success'
        })
      }, 800)
    } else {
      setTimeout(() => {
        wx.hideLoading()
        wx.showModal({
          title: '支付失败',
          showCancel: false,
          content: postRes.message,
        })
      }, 800)
    }
  },
  // 申请退款
  async refund() {

    const orderId = this.data.order.id
    const {
      cancel
    } = await wx.showModal({
      title: '申请退款',
      content: '是否申请退款？',

    })
    if (cancel) return
    wx.showLoading({
      title: '申请中..'
    })
    const postRes = await postStoreOrderRefundApi({
      orderId
    })
    console.log(postRes)
    if (postRes.code == 200) {
      setTimeout(async () => {
        await this.getOrder(orderId)
        await this.getInfo()
        this.onClose()
        wx.showToast({
          title: '已申请退款',
          icon: 'none'
        })
      }, 800)
    } else {
      setTimeout(() => {
        wx.hideLoading()
        wx.showModal({
          title: '申请失败',
          showCancel: false,
          content: postRes.message,
        })
      }, 800)
    }
  },
  // 确认收货
  async confirmReceipt() {
    const orderId = this.data.order.id
    const {
      cancel
    } = await wx.showModal({
      title: '确认收货',
      content: '是否确认收货？',

    })
    if (cancel) return
    const postRes = await postStoreOrderConfirmApi({
      orderId
    })
    console.log(postRes)
    if (postRes.code == 200) {
      setTimeout(async () => {
        await this.getOrder(orderId)
        await this.getInfo()
        this.onClose()
        wx.showToast({
          title: '已确认收货',
          icon: 'none'
        })
      }, 800)
    } else {
      setTimeout(() => {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: postRes.message,
        })
      }, 800)
    }
  }

})